package com.jxtti.service;

import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.jxtti.dao.SubjectMenuDao;
import com.jxtti.entity.SubjectMenu;
/**
 * 
 * <br>
 * <b>功能：</b>SubjectMenuService<br>
 * <b>作者：lwb</b><br>
 * <b>日期：</b> Nov 26, 2015 <br>
 * <b>版权所有：<b>版权所有(C) 2015，<br>
 */
@Service("subjectMenuService")
@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
public class SubjectMenuService {

	private final static Logger log= Logger.getLogger(SubjectMenuService.class);
	

	

	@Autowired
    private SubjectMenuDao subjectMenuDao;

		


	/**
	 * 获取信息数据列表
	 * 
	 * @param data
	 * @return
	 */
	public Map<String, Object> list(Map<String, Object> data,  SubjectMenu subjectMenu)
	{
		//int rowCount = subjectMenuDao.queryByCount(subjectMenu);//。queryByCount(tMonitorProjectInfo);
		//subjectMenu.getPager().setRowCount(rowCount);
		//List<SubjectMenu> lstSubjectMenu  = subjectMenuDao.queryByList(data);
	
	
	
	
		int pageNum = Integer.parseInt((data.get("currentPage") == null || data
				.get("currentPage").equals("0")) ? "1" : data.get("currentPage").toString());
		// 每页显示条数 
		int pageSize = Integer.parseInt((data.get("pageSize") == null || data.get("pageSize").equals("0")) ? "10" : data.get("pageSize").toString());
		PageHelper.startPage(pageNum, pageSize);
		PageInfo<SubjectMenu> pages = new PageInfo<SubjectMenu>( subjectMenuDao.queryByList(data));
		data.put("page", pages);
		return data;
	}
	
	
	
	/**
	 * 查询所有数据。 
	 * @param data
	 * @return
	 */
	public List<SubjectMenu>  queryAll(Map<String, Object> data)
	{
		return  subjectMenuDao.queryByList(data);
	}
	
	
	
	/**
	 * 按Seq排序.
	 * @param data
	 * @return
	 */
	public List<SubjectMenu> queryListByOrderSeq(Map<String, Object> data)
	{
		return subjectMenuDao.queryListByOrderSeq(data);
	}
	
	
	/**
	 * 
	 * @param data
	 * @return
	 */
	public  List<Map<String, Object>>  getEasyUiTree(Map<String, Object> data)
	{
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		List<SubjectMenu> list=new ArrayList<SubjectMenu>(); 
		
		if (null == data.get("parent_id")) {
			data.put("parent_id", "0");
		}	
		
		list.addAll(this.subjectMenuDao.queryByList(data));
		
		if (null != list && 0 != list.size()) { //判断是否有数据
			for (int i = 0; i < list.size(); i++) { //循环数据
				
				Map<String, Object> node = new HashMap<String, Object>();					
				node.put("id", list.get(i).getId()); //是 getCode	
				node.put("parent_id", list.get(i).getParent_id());	
				node.put("title", list.get(i).getTitle());					
				node.put("seq", list.get(i).getSeq());	
				node.put("summary", list.get(i).getSummary());
			
				
				/** 自定义属性
				    Map<String, Object> attrNode = new HashMap<String, Object>();							
					attrNode.put("code", list.get(i).getCode());				
					node.put("attributes",attrNode);
				 */		
				
				//判断是否有子节点,
				SubjectMenu  smenu = new SubjectMenu();
				smenu.setParent_id(list.get(i).getId());
				
				if(this.subjectMenuDao.queryByCount(smenu) > 0)
				{
					// 区域节点，需要展开下级
					List<Map<String, Object>> childResult = new ArrayList<Map<String, Object>>();
					
					Map<String, Object> childData=data; //传递 type 和status

					childData.put("parent_id", list.get(i).getId().toString());//参数 父类 改变了	

					childResult=getEasyUiTree(childData);//---------------递归 			
		
					node.put("children", childResult);
				}
					
				/*if ("true".equals(list.get(i).getParent_id())) { //是父级节点
					// 区域节点，需要展开下级
					List<Map<String, Object>> childResult = new ArrayList<Map<String, Object>>();
					
					Map<String, Object> childData=data; //传递 type 和status
					System.out.println(list.get(i).getId());
					childData.put("parent_id", list.get(i).getId().toString());//参数 父类 改变了	
					
					
					childResult=getEasyUiTree(childData);//---------------递归 			
		
					node.put("children", childResult);
				}		*/		
				result.add(node);
			}
		}
		return result;
	}
	
	
	
	/**
	 * 添加数据.
	 * 
	 * @param data
	 * @return
	 */
	public int add(SubjectMenu subjectMenu)
	{
		int insertCount = subjectMenuDao.add(subjectMenu);
		return insertCount;
	}
	

	/**
	 * 修改数据.
	 * 
	 * @param data
	 * @return
	 */
	public int update(SubjectMenu subjectMenu)
	{
		int updateCount = subjectMenuDao.update(subjectMenu);
		return updateCount;
	}
	

	/**
	 * 删除数据.
	 * 
	 * @param data
	 * @return
	 */
	public int delete(SubjectMenu subjectMenu)
	{
		int deleteCount = subjectMenuDao
				.delete(subjectMenu);
		
		return deleteCount;
	}
	
	
	public SubjectMenu querySubjectMenuById(SubjectMenu subjectMenu)
	{
		subjectMenu = subjectMenuDao.querySubjectMenuById(subjectMenu);
		return subjectMenu;
	}

	
	public List<SubjectMenu> queryMenuTree(Map<String, Object> data)
	{
		return this.subjectMenuDao.queryMenuTree(data);
	}
}
